<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use org\Agent;
use org\Ip;
class Pub extends Controller
{

    private $obj;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);

        /**
         * 加载这里就退出
         */
        if (session('Admin', '', 'admin')) {
            session(null, 'admin');
            $this->redirect('pub/login');
        }
        $this->obj = model('Admin');
    }

    /**
     * 跨站登录
     */
    public function weburl()
    {

            $data = $this->request->param();

            $result = $this->validate($data, 'Admin.weburl');
            if (true !== $result) {
                return ($result);
            }

            $user = model('Admin')->get(['name' => $data['name']]);

            if (!$user) {
                return ("用户不存在");
            }

            if ($user->password != md5($data['password'] . $user->code)) {
                return ("密码不正确");
            }

            $data = ['last_login_time' => time()];

            model('Admin')->updatalosttime($data, $user->id);
            session('Admin', $user, 'admin');

            return $this->redirect('index/index');

    }

    /**
     * 发送请求,第二个参数有值时为Post请求
     *
     * @param string $url 请求地址
     * @param array $fields 发送的post表单
     *
     * @return string
     */
    public function curl( $url, $fields = [ ] ) {
        $ch = curl_init();
        //设置我们请求的地址
        curl_setopt( $ch, CURLOPT_URL, $url );
        //数据返回后不要直接显示
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
        //禁止证书校验
        curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
        curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false );
        if ( $fields ) {
            curl_setopt( $ch, CURLOPT_TIMEOUT, 30 );
            curl_setopt( $ch, CURLOPT_POST, 1 );
            curl_setopt( $ch, CURLOPT_POSTFIELDS, $fields );
        }
        $data = '';
        if ( curl_exec( $ch ) ) {
            //发送成功,获取数据
            $data = curl_multi_getcontent( $ch );
        }
        curl_close( $ch );

        return $data;

    }

    public function login()
    {

        if ($this->request->isPost()) {

            $data = $this->request->post();
            if(!empty($data['weburl'])){
                $url  =$data['weburl'] . '-name-' . $data['name'] . '-password-' . $data['password'];
                $data = $this->curl( $url );
                if($data){
                    return ajax_info($data);
                }
                return ajax_info('等待跳转','',$url);
            }


            $result = $this->validate($data, 'Admin.pub');
            if (true !== $result) {

                return ajax_info($result);
            }

            $user = model('Admin')->get(['name' => $data['name']]);

            if (!$user) {

                return ajax_info("用户不存在");
            }

            if ($user->password != md5($data['password'] . $user->code)) {
                return ajax_info("密码不正确");
            }

            $data = ['last_login_time' => time()];

            model('Admin')->updatalosttime($data, $user->id);
            session('Admin', $user, 'admin');
            // 记录登录日志
            $log['uid'] = $user->id;
            $log['name'] = $user->name;
            $log['login_ip'] = $this->request->ip();
            $log['login_location'] = implode(" ", Ip::find($log['login_ip']));
            $log['login_browser'] = Agent::getBroswer();
            $log['login_os'] = Agent::getOs();
            db("loginlog")->insert($log);

            return ajax_info("登录成功", 'y');
        }

        $weburl = model('weburl')->where('status','1')->field('title,url')->select();
        $this->assign('weburl',$weburl);
        return $this->fetch();
    }


}
